﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace NextViewer
{
    public struct Node
    {
        int id;
        public double x;
        public double y;
        public double z;
        public int Color;
        public Edges edge;

        public Node(int i,double xx,double yy, double zz, Edges ee)
        {
            id = i; x = xx; y=yy;
            z = zz; edge = ee;
            Color = 0;
        }
    }
    public class Nodes
    {
        public Nodes()
        {
            nodes = new Hashtable();
        }
        public void Add(int id, double x, double y, double z, Edges e)
        {
            if (nodes.Contains(id))
            {
                Node nd = ((Node)nodes[id]);
                nd.z+=z;
                foreach (DictionaryEntry de in e)
                {
                    nd.edge.Add((int)de.Key);
                }
                nodes.Remove(id);
                nodes.Add(id, nd);
            }
            else
            {
                Node nd = new Node(id, x, y, z, e);
                nodes.Add(id, nd);
            }

            
        }

        public void StartTravel()
        {
            int i=0;
            nodeArr = new Node[nodes.Count];
            foreach (DictionaryEntry ed  in nodes)
            {
                Node nd = (Node)ed.Value;
                nodeArr[i] = nd;
                i++;
            }
            QuickSort(0, nodes.Count-1, 0);
            nid = 0;
        }
        
        public Node GetNext()
        {
            return (Node)nodeArr[nid++];
        }
        public Node GetNode(int id)
        {
            return (Node)nodes[id];
        }
        public int Count()
        {
            return nodes.Count;
        }
        public void QuickSort(int low, int high, int bound)
        {
            if (low > bound)
                return;
            int i = low, j = high;
            Node pTemp;
            //Chro2* pop = pop;
            // Get the pivot element from the middle of the list
            double pivot = nodeArr[(low + high) / 2].z;

            // Divide into two lists
            while (i <= j)
            {
                while (nodeArr[i].z > pivot)
                {
                    i++;
                }
                while (nodeArr[j].z < pivot)
                {
                    j--;
                }

                if (i <= j)
                {
                    //Swap(&pop[i],&pop[j]);
                    pTemp = nodeArr[i];
                    nodeArr[i] = nodeArr[j];
                    nodeArr[j] = pTemp;

                    i++;
                    j--;
                }
            }
            // Recursion
            if (low < j)
                QuickSort(low, j, bound);
            if (i < high)
                QuickSort(i, high, bound);
        }
        int nid;
        public Node[] nodeArr;
        public Hashtable  nodes;
        
    }
    public struct Edge
    {
        public int id;
        public int value;
        public Edge(int i, int v)
        {
            id = i; value = v;
        }
    }
    public class Edges
    {

        public Edges()
        {
            edges = new Hashtable();
        }
        public void Add(int dd)
        {
            if (edges.Contains(dd))
            {
                Edge ee = (Edge)edges[dd];
                ee.value++;
                edges.Remove(dd);
                edges.Add(dd,ee);

            }
            else
            {
                Edge ee = new Edge(dd,1);
                edges.Add(dd, ee);
            }
        }
        public int Count()
        {
            return edges.Count;
        }
        public IDictionaryEnumerator GetEnumerator()
        {
            return edges.GetEnumerator();
        }
        public Hashtable edges;
    }
}
